package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcChargeModel;
import com.csun.cmny.provider.model.domain.OmcIetypeConfig;
import com.csun.cmny.provider.model.dto.OmcChargeModelDto;
import com.csun.cmny.provider.model.dto.OmcIetypeConfigDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcChargeModelService;
import com.csun.cmny.provider.service.OmcIetypeConfigService;
import com.csun.cmny.provider.validator.OmcChargeModelValidator;
import com.csun.cmny.provider.validator.OmcFoodValidator;
import com.csun.cmny.provider.vo.OmcChargeConfigVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: chenzheng
 * @Date: 2019-08-13
 * @Description: 费用类别配控制器
 */
@RestController
@RequestMapping(value = "/chargeConfig")
public class OmcChargeConfigController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcChargeModelService omcChargeModelService;

    @Resource
    private OmcIetypeConfigService  omcIetypeConfigService;


    /**
     * 添加收费模块信息
     * @param omcChargeModelDto
     * @param br
     * @return
     */
    @ValidateAnnotation
    @RequestMapping(value = "/addChargeModel", method = RequestMethod.POST)
    public Wrapper addChargeModel(@Validated(value = {OmcChargeModelValidator.class }) @RequestBody OmcChargeModelDto omcChargeModelDto, BindingResult br) {
        omcChargeModelDto.setGroupId(getLoginAuthDto().getGroupId());
        //验证需添加的收费模块名是否已存在
        OmcChargeModel omcChargeModel = new OmcChargeModel();
        omcChargeModel.setChargeModelName(omcChargeModelDto.getChargeModelName());
        omcChargeModel.setGroupId(getLoginAuthDto().getGroupId());
        int m = omcChargeModelService.selectCount(omcChargeModel);
        if(m > 0){//需添加的收费模块名已存在,不能重复添加.
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("charge.model.exist.error", null), m);
        }
        int n = saveOmcChargeModel(omcChargeModelDto);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.insert.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.insert.success", null), n);
    }


    /**
     * 通过ID查询收费模块名
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectChargeModelById", method = RequestMethod.POST)
    public Wrapper selectChargeModelById(Long id) {
        //验证参数
        if(null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        OmcChargeModel omcChargeModel = omcChargeModelService.selectByKey(id);
        if (null == omcChargeModel) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), omcChargeModel);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), omcChargeModel);
    }

    /**
     * 通过ID删除收费模块信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteChargeModelById", method = RequestMethod.POST)
    public Wrapper deleteChargeModelById(Long id) {
        //验证参数
        if(null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        int n = omcChargeModelService.deleteByKey(id);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.delete.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.delete.sucess", null), n);
    }

    /**
     * 通过收费模块名模糊查询收费模块信息
     * @param omcChargeModelDto
     * @return
     */
    @RequestMapping(value = "/selectChargeModelByName", method = RequestMethod.POST)
    public Wrapper selectChargeModelByName(@RequestBody OmcChargeModelDto omcChargeModelDto) {
        omcChargeModelDto.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcChargeModel> list = omcChargeModelService.selectByName(omcChargeModelDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

    /**
     * 费用类别配置
     * @param omcIetypeConfigDto
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@RequestBody OmcIetypeConfigDto omcIetypeConfigDto) {
        //验证参数
        if(null == omcIetypeConfigDto.getChargeModelId() || 0 == omcIetypeConfigDto.getChargeModelId()){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("charge.model.id.is.not.null", null), null);
        }
        //验证收费模块是否存在
        OmcChargeModel omcChargeModel = omcChargeModelService.selectByKey(omcIetypeConfigDto.getChargeModelId());
        if(null == omcChargeModel){
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("charge.model.is.not.exist", null), null);
        }
        List<OmcIetypeConfig> list = new ArrayList<>();
        List<Long> itemIdList = omcIetypeConfigDto.getItemIdlist();
        //判断绑定的费用项目是否存在
        if(null !=itemIdList && itemIdList.size() > 0) {
            for (Long itemId: itemIdList) {
             if(!omcIetypeConfigService.isChargeItem(itemId)){
                 return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("charge.model.item.is.not.exist", null), itemId);
             }
            }
        }
        OmcIetypeConfig omcIetypeConfig = new OmcIetypeConfig();
        omcIetypeConfig.setGroupId(getLoginAuthDto().getGroupId());
        omcIetypeConfig.setChargeModelId(omcIetypeConfigDto.getChargeModelId());
        //删除收费模块已配置的费用项目
        omcIetypeConfigService.delete(omcIetypeConfig);
        if(null !=itemIdList && itemIdList.size() > 0){
        for (Long itemId: itemIdList) {
            OmcIetypeConfig dto = new OmcIetypeConfig();
            dto.setGroupId(getLoginAuthDto().getGroupId());
            dto.setChargeModelId(omcIetypeConfigDto.getChargeModelId());
            dto.setItemId(itemId);
            dto.setUpdateInfo(getLoginAuthDto());
            list.add(dto);
        }
        }
         int  n = omcIetypeConfigService.batchSave(list);
        if(n <= 0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("charge.model.bind.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("charge.model.bind.success", null), n);
    }



    /**
     * 保存或者更新收费模块信息
     *
     * @param: [uacPlanDto]
     * @return: java.lang.Integer
     */
    private Integer saveOmcChargeModel(OmcChargeModelDto omcChargeModelDto) {
        OmcChargeModel omcChargeModel = new OmcChargeModel();
        BeanUtils.copyProperties(omcChargeModelDto, omcChargeModel);
        return omcChargeModelService.saveOmcChargeModel(omcChargeModel,getLoginAuthDto());
    }

    /**
     * 通过ID删除费用类别配置信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Wrapper deleteById(Long id) {
        //验证参数
        if(null == id || id <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        int n = omcIetypeConfigService.deleteByKey(id);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.delete.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.delete.sucess", null), n);
    }

    /**
     * 修改费用类别配置信息
     * @param omcIetypeConfig
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody OmcIetypeConfig omcIetypeConfig) {
        //验证参数
        if(null == omcIetypeConfig.getId() || omcIetypeConfig.getId() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("id.empty", null), null);
        }
        omcIetypeConfig.setGroupId(getLoginAuthDto().getGroupId());
        omcIetypeConfig.setUpdateInfo(getLoginAuthDto());
        int n = omcIetypeConfigService.update(omcIetypeConfig);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.change.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.change.success", null), n);
    }

    /**
     * 查询费用类别配置列表
     * @return
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public Wrapper queryList() {
        List<OmcChargeConfigVo> list = omcIetypeConfigService.queryALL(getLoginAuthDto().getGroupId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.query.success", null), list);
    }

}
